import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map'
import { Park } from './park';


@Injectable()
export class ParkDataProvider {
  private readonly baseUrl: string = 'https://api.bmob.cn/1';
  private readonly httpHeaders : HttpHeaders = new HttpHeaders({
    'X-Bmob-Application-Id':'9731cfbede01e27cc49b3b9e989556d5',
    'X-Bmob-REST-API-Key':'5fc3e4d93fd90418fc9cab62a6a07216',
    'Content-Type':'application/json'
  });
  

  constructor(public http: HttpClient) {
    
  }
  
  /**
   * 分页获取景区数据
   * @param num 
   * @param page 
   */
  getParks(num: number, page: number): Observable<Park[]> {
    let _params = new HttpParams()
    .set('limit',`${num}`)
    .set('skip',`${(page - 1) * num}`);
    return this.http.get<Park[]>(`${this.baseUrl}/classes/park`,
    {headers: this.httpHeaders, params: _params}).map(_=>_['results']);
    }

    /**
     * 获取一条景区记录
     * @param objectId 
     */
    getPark(objectId: string): Observable<Park>{
      return this.http.get<Park>(`${this.baseUrl}/classes/park/${objectId}`,
      {headers: this.httpHeaders});
    }
}
